package com.chatify.me;



import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;


public class LoginActivity extends BaseActivity {
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		Button login = (Button) this.findViewById(R.id.login);
		login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Connect();
            }
        });
		
		Button newuser = (Button) this.findViewById(R.id.btnNewUser);
		newuser.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Intent i=new Intent(LoginActivity.this,RegisterNewUser.class);
            	startActivity(i);
            }
        });
		 
	}
	
	
	
	public void Connect()
	{
 
	
	ConnectionModel cmodel = new ConnectionModel();
	cmodel.hostName = this.getString(R.string.Host);
	cmodel.portNumber = Integer.parseInt(this.getString(R.string.ClientPort));
	cmodel.serviceName = this.getString(R.string.Service);
	cmodel.userName = this.getEditText(R.id.userid);
	cmodel.password = this.getEditText(R.id.password);
	 
	SessionMaster.StoreUserInfo(cmodel.userName, cmodel.password,LoginActivity.this);
	NetworkConnection nConn = new NetworkConnection(cmodel,true);
	
	
	 if(NetworkConnection.xmppConnection!=null)
	    {
		
	    		      if(NetworkConnection.xmppConnection.isConnected())
	    		      {
	    				Intent intent = new Intent(LoginActivity.this,UsersListActivity.class);
	    			
	    				
	    				startActivity(intent);	
	    		      }
	    		      else
	    		      {
	    		    	  Toast.makeText(LoginActivity.this, "Failed to log in", Toast.LENGTH_LONG).show();
	    		      }
	    		      }
	    		      else
	    		      {
	    		    	  Toast.makeText(LoginActivity.this, "Failed to log in ", Toast.LENGTH_LONG).show();
	    		 	     
	    		      }
	}
	
	    	public  String getEditText(int id){
	    		
	    		EditText text=(EditText) this.findViewById(id);
	    		return text.getText().toString();
	    	}
	
}
